package com.student_system.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.student_system.system.entity.SysRole;
import com.student_system.system.entity.SysUserRole;
import com.student_system.system.mapper.SysRoleMapper;
import com.student_system.system.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.student_system.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljy
 * @since 2025-05-14
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    ISysUserRoleService sysUserRoleService;

    @Override
    public void addRole(SysRole sysRole) {
        //将新增的角色保存到角色表
        this.save(sysRole);
        //将新增的角色保存到角色用户中间表（为超级管理员添加对应关系）
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(sysRole.getRoleId());
        sysUserRole.setUserId(1);
        sysUserRoleService.save(sysUserRole);
    }

    @Override
    public Page<SysRole> queryRolePage(Page<SysRole> page, String roleName) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(roleName)) {
            queryWrapper.like(SysRole::getRoleName, roleName);
        }
        return this.page(page, queryWrapper);
    }
}
